package com.xh.sys.service.impl;


import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.xh.sys.entity.SysCommentBack;
import com.xh.sys.mapper.SysCommentBackMapper;
import com.xh.sys.service.SysCommentBackService;
import com.xh.sys.vo.webform.SysCommentBackQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 浏览评论回复功能下载_关联信息表 服务实现类
 * </p>
 *
 * @author lhl
 * @since 2023-10-19
 */
@Service
public class SysCommentBackServiceImp extends ServiceImpl<SysCommentBackMapper, SysCommentBack> implements SysCommentBackService {

	/**
	 * 创建评论浏览下载主题
	 * 1外联的查询 查到返回查到的 查不到新增
	 * 更新浏览次数,下载次数有值的 没值的直接退出
	 *
	 * @param sysCommentBack
	 * @return
	 */
	@Transactional
	@Override
	public boolean saveSysCommentBack(SysCommentBack sysCommentBack) {

		SysCommentBackQuery build = SysCommentBackQuery.builder()
				.type(sysCommentBack.getType())
				.key(sysCommentBack.getKey())
				.value(sysCommentBack.getValue())
				.build();

		SysCommentBack sysCommentBack1 = this.queryByForeign(build);
		if (null == sysCommentBack1) {

			boolean save = this.save(sysCommentBack);

			if (null == sysCommentBack.getViewedNumber()) {
				sysCommentBack.setViewedNumber(1);
			}
			if (null == sysCommentBack.getDownloadNumber()) {
				sysCommentBack.setDownloadNumber(0);
			}
			return save;
		} else {
			sysCommentBack.setId(sysCommentBack1.getId());
			sysCommentBack.setViewedNumber(sysCommentBack1.getViewedNumber());
			sysCommentBack.setDownloadNumber(sysCommentBack1.getDownloadNumber());
			return true;
		}
	}


	@Override
	public SysCommentBack queryByForeign(SysCommentBackQuery sysCommentBackQuery) {

		return this.getOne(sysCommentBackQuery.queryWrapperBuild());
	}
}
